package com.cy.store.service.impl;

import com.cy.store.entity.Cart;
import com.cy.store.entity.Product;
import com.cy.store.mapper.CartMapper;
import com.cy.store.mapper.ProductMapper;
import com.cy.store.service.ICartService;
import com.cy.store.service.ex.AccessDeniedException;
import com.cy.store.service.ex.CartNotFoundException;
import com.cy.store.service.ex.InsertException;
import com.cy.store.service.ex.UpdateException;
import com.cy.store.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl implements ICartService {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addToCart(Integer uid, Integer pid, String username, Integer amount) {
        //查询购物车中是否存在要添加的商品
        Cart cart = cartMapper.findByUidAndPid(uid,pid);
        if(cart==null){//表示这个商品不在购物车中，需要添加
            Cart c = new Cart();
            c.setPid(pid);
            c.setUid(uid);
            c.setNum(amount);

            Product product = productMapper.findById(pid);
            c.setPrice(product.getPrice());
            c.setCreatedTime(new Date());
            c.setCreatedUser(username);
            c.setModifiedTime(new Date());
            c.setModifiedUser(username);

            Integer len = cartMapper.insert(c);
            if(len!=1){
                throw new InsertException("插入数据产生未知异常");
            }
        }else{//表示购物车中已存在此商品，则更新这条数据的num
            Integer num = cart.getNum()+amount;

            Integer len = cartMapper.updateNumByCid(cart.getCid(),num,username,new Date());

            if(len!=1){
                throw new UpdateException("更新数据时产生未知异常");
            }

        }
    }

    @Override
    public List<CartVo> getVoByUid(Integer uid) {
        List<CartVo> list = cartMapper.findByUid(uid);

        return list;
    }

    @Override
    public Integer addNum(Integer cid, Integer uid, String username) {
        Cart result = cartMapper.findByCid(cid);
        if(result==null){
            throw new CartNotFoundException("购物车数据不存在");
        }
        if(!result.getUid().equals(uid)){
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num = result.getNum()+1;
        Integer len = cartMapper.updateNumByCid(cid,num,username,new Date());
        if(len!=1){
            throw new UpdateException("更新数据出现未知异常");
        }

        return  num;
    }

    @Override
    public Integer reduceNum(Integer cid, Integer uid, String username) {
        Cart result = cartMapper.findByCid(cid);
        if(result==null){
            throw new CartNotFoundException("购物车数据不存在");
        }
        if(!result.getUid().equals(uid)){
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num;
        if(result.getNum()>0){
            num = result.getNum()-1;
        }else{
            num=0;
        }

        Integer len = cartMapper.updateNumByCid(cid,num,username,new Date());
        if(len!=1){
            throw new UpdateException("更新数据出现未知异常");
        }

        return  num;
    }

    @Override
    public List<CartVo> getVoByCids(Integer[] cids, Integer uid) {
        List<CartVo> list = cartMapper.findVoByCids(cids);
        for(CartVo cv : list){
            if(!cv.getUid().equals(uid)){
                list.remove(cv);
            }
        }
        return list;
    }


}
